package cn.lyjuan.payment.third.http.api.config;

import cn.lyjuan.base.util.SpringContextHolder;
import cn.lyjuan.payment.third.base.pay.cst.PayConst;
import com.hehewang.hhw.tdmq.client.producer.AMQProducer;
import com.hehewang.hhw.tdmq.client.producer.DefaultMQProducerImpl;
import com.hehewang.hhw.tdmq.dao.ClientDao;
import com.hehewang.hhw.tdmq.enums.ClientTypeEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author chad
 */
@Configuration
@Component
public class TdmqConfig {
    @Resource
    private PayConst payConst;

    @Value("${mq.topics}")
    private String topics;
    @Value("${mq.name}")
    private String name;
    @Value("${mq.service.url}")
    private String serviceUrl;
    @Value("${mq.listener.name}")
    private String listenerName;
    @Value("${ma.role.token}")
    private String roleToken;
    /**
     * 强制在调用前加载配置
     */
    @Resource
    private SpringContextHolder holder;

    @Bean("orderMq")
    public AMQProducer orderMq() {
        ClientDao config = new ClientDao();
        config.setIsTest(payConst.isPayDebug());
        config.setType(ClientTypeEnum.PRODUCER);
        config.setTopics(topics);
        config.setName(name);
        config.setServiceUrl(serviceUrl);
        config.setListenerName(listenerName);
        config.setRoleToken(roleToken);
        DefaultMQProducerImpl producer = new DefaultMQProducerImpl(config);
        producer.start();
        return producer;
    }
}
